package com.ruoyi.business.rescueTask.service.impl;

import com.ruoyi.business.rescueTask.service.NearbyService;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.connection.RedisGeoCommands.GeoRadiusCommandArgs;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 盖昭昭
 * @Date 2024/12/25 21:54
 * @Description NearbyServiceImpl
 * @Version Ultimate
 */
@Service("/nearbyService")
public class NearbyServiceImpl implements NearbyService {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Override
    public void nearbyJoin() {
       /* zhangsan 113.558578 34.825800
        lisi     113.667166 34.751665
        wGngwu   113.635067 34.868832
        xiaoli   114.371185 34.814882
        xiaohong 112.416143 34.601364*/
        redisTemplate.opsForGeo().add("nearby",new Point(113.558578,34.825800),"1");
        redisTemplate.opsForGeo().add("nearby",new Point(113.667166,34.751665),"2");
        redisTemplate.opsForGeo().add("nearby",new Point(113.635067,34.814882),"3");
        redisTemplate.opsForGeo().add("nearby",new Point(114.371185,34.825800),"4");
        redisTemplate.opsForGeo().add("nearby",new Point(112.416143,34.601364),"5");

    }

    public GeoResults<RedisGeoCommands.GeoLocation<Object>> nearbyLeave(Double longitude, Double latitude){
        Point center = new Point( longitude, latitude);//定义中心点
        Distance radius = new Distance(50, Metrics.KILOMETERS);//定义
        Circle within=new Circle(center,radius);
        // 设置 GeoRadiusCommandArgs，要求返回距离
        RedisGeoCommands.GeoRadiusCommandArgs args = GeoRadiusCommandArgs.newGeoRadiusArgs().includeDistance();
        GeoResults<RedisGeoCommands.GeoLocation<Object>> results = redisTemplate.opsForGeo().radius("nearby", within, args);
        return results;
    }
}
